<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2022-01-20 09:42:31
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-05-07 08:48:16
-->
<template>
  <div class="app-container">
    <div class="header">
      <div class="bread">
        <p>自动化规则</p>
      </div>
      <div class="ipt">
        <div>
          <el-input
            v-model="input"
            placeholder="请输入任务名称"
            size="normal"
            clearable
          ></el-input>
        </div>
        <div>
          <el-button type="primary" size="default" @click="search">
            搜索
          </el-button>
        </div>
      </div>
      <div class="btn"></div>
    </div>
    <div class="chooise">
      <span>场景：</span>
      <el-select
        v-model="value"
        ref="sel"
        clearable
        @visible-change="(v) => visibleChange(v, 'sel', selClick, admClick)"
        class="sel-sence"
        @change="querySceneList"
      >
        <el-option
          v-for="item in options"
          :key="item.sceneId"
          :label="item.name"
          :value="item.sceneId"
        >
        </el-option>
      </el-select>
      <el-button
        type="primary"
        icon="iconfont icon-shaixuan"
        size="default"
        @click="screen"
      >
        高级筛选
      </el-button>
    </div>
    <div v-if="scenArr !== []" class="scenF">
      <div v-for="(item, index) in scenArr" :key="index" class="scenF-content">
        <div class="scenF-detail" @click="scenSearch(item.sceneId)">
          {{ item.sceneList }}
          <i
            class="el-icon-circle-close"
            @click.stop="scenDel(index)"
            style="margin-left: 10px"
          ></i>
        </div>
      </div>
    </div>
    <el-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      height="490"
    >
      <template slot="empty" class="emptyBg">
        <img src="@/assets/images/empty.jpg" alt="" />
      </template>
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="所属任务" prop="taskName"></el-table-column>
      <el-table-column label="负责人" prop="userName"></el-table-column>
      <el-table-column label="开始时间" prop="startTime"></el-table-column>
      <el-table-column
        label="执行规则"
        prop="rule"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column label="已执行次数" prop="frequency"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <span @click="editRule(scope.row)">编辑</span>
          <span @click="toggleStatus(scope.row)">
            {{ scope.row.enable == 1 ? "停止" : "启用" }}
          </span>
          <span @click="del(scope.row)">删除</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      prev-text="上一页"
      next-text="下一页"
      background
      :page-size="pageSize"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      layout=" ->,total, sizes, prev, pager, next, jumper "
      :total="total"
    >
    </el-pagination>
    <el-dialog
      title="编辑自动化规则"
      :visible.sync="dialog.cronShow"
      width="30%"
    >
      <cycle ref="cycleRules"></cycle>
      <span slot="footer">
        <el-button @click="dialog.cronShow = false">取消</el-button>
        <el-button type="primary" @click="cronSubmit">确定</el-button>
      </span>
    </el-dialog>
    <drag-table
      :dialog="dialog"
      v-if="dialog.senceAdminEject"
      ref="dragTable"
    ></drag-table>
    <screen
      :dialog="dialog"
      :switchShow="switchShow"
      @sceneArr="sceneArr"
    ></screen>
  </div>
</template>

<script>
import {
  ruleList,
  editRule,
  delRule,
  startRule,
  stopRule,
} from "@/api/task/loopTask";
import { queryScene } from "@/api/currency";
import screen from "./automation/screen.vue";
import dragTable from "./automation/dragTable.vue";
import Cycle from "./Cycle.vue";
export default {
  components: { screen, dragTable, Cycle },
  data() {
    return {
      options: [],
      tableData: [],
      scenArr: [],
      input: "",
      value: "",
      selShow: true,
      pageSize: 10,
      total: 0,
      pageNum: 1,
      switchShow: [
        {
          value: true,
          label: "所属任务",
          stage: "taskName",
        },
        {
          value: true,
          label: "负责人",
          stage: "userName",
        },
      ],
      dialog: {
        aboutShow: false,
        aboutShowEject: false,
        cronShow: false,
        newLoopTaskShow: false,
        newLoopTaskShowEject: false,
        dialogTableVisible: false,
        dialogScreenVisible: false,
        dialogPerCha: false,
        dialogEnter: false,
        senceAdmin: false, //场景管理
        senceAdminEject: false,
        distributionShow: false,
        distributionShowEject: false,
      },
    };
  },
  created() {
    this.getList();
    this.getScene();
  },
  watch: {
    input(newValue, oldValue) {
      if (newValue == "") {
        this.getList();
      }
    },
  },
  methods: {
    // 获取高级筛选
    getScene() {
      queryScene({ type: 42 }).then((res) => {
        if (res.default !== null) {
          res.scenes.unshift(res.default);
          this.options = res.scenes;
        } else {
          this.options = res.scenes;
        }
      });
    },
    cronSubmit() {
      if (this.$refs["cycleRules"].cycleForm.type == 1) {
        this.$refs[
          "cycleRules"
        ].cycleForm.rule = `该任务将于每${this.$refs["cycleRules"].cycleForm.interval}天自动创建`;
      } else if (this.$refs["cycleRules"].cycleForm.type == 2) {
        this.$refs[
          "cycleRules"
        ].cycleForm.rule = `该任务将于每${this.$refs["cycleRules"].cycleForm.interval}周的${this.$refs["cycleRules"].specificStr}自动创建`;
      } else if (this.$refs["cycleRules"].cycleForm.type == 3) {
        this.$refs[
          "cycleRules"
        ].cycleForm.rule = `该任务将于每${this.$refs["cycleRules"].cycleForm.interval}月的${this.$refs["cycleRules"].specificStr}自动创建`;
      }
      editRule(this.$refs["cycleRules"].cycleForm).then((res) => {
        this.$message.success(res.msg);
        this.dialog.cronShow = false;
        this.getList();
      });
    },
    // 关闭规则
    ruleClose() {
      this.dialog.cronShow = false;
    },
    // 编辑
    editRule(row) {
      this.dialog.cronShow = true;
      this.$nextTick(function () {
        this.$refs["cycleRules"].cycleForm = row;
        if (row.type == 2) {
          this.$refs["cycleRules"].weekChange(row.dateArray);
        } else if (row.type == 3) {
          this.$refs["cycleRules"].mouthChange(row.dateArray);
        }
      });
    },
    //   切换状态
    async toggleStatus(row) {
      const confirmResult = await this.$confirm(
        "此操作将修改该任务状态, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      /*如果用户确认打印confirm,如果用户取消显示cancel*/
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除!");
      } else {
        if (row.enable == 1) {
          stopRule({
            taskId: row.taskId,
          }).then((res) => {
            this.$message.success(res.msg);
            this.getList();
          });
        } else if (row.enable == 2) {
          startRule({
            taskId: row.taskId,
          }).then((res) => {
            this.$message.success(res.msg);
            this.getList();
          });
        }
      }
    },
    // 删除
    async del(row) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该任务, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      /*如果用户确认打印confirm,如果用户取消显示cancel*/
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除!");
      } else {
        delRule({
          taskId: row.taskId,
        }).then((res) => {
          this.$message.success(res.msg);
          this.getList();
        });
      }
    },
    // 点击临时搜索
    scenSearch(id) {
      this.getList(id);
    },
    // 删除临时搜索
    scenDel(i) {
      this.scenArr.splice(i, 1);
      if (this.scenArr.length == 0) {
        this.getList();
      }
    },
    // 获取数组
    sceneArr(data) {
      this.scenArr.push(data);
    },
    querySceneList() {
      this.getList(this.value);
    },
    //管理
    admClick() {
      this.dialog.senceAdminEject = true;
      this.$nextTick(function () {
        this.dialog.senceAdmin = true;
      });
    },
    //新建场景
    selClick() {
      this.screen();
    },
    // 场景下拉框按钮
    visibleChange(visible, refName, onClick, onClick2) {
      if (visible) {
        const ref = this.$refs[refName];
        let popper = ref.$refs.popper;
        if (popper.$el) popper = popper.$el;
        if (
          !Array.from(popper.children).some(
            (v) => v.className === "el-cascader-menu__list"
          )
        ) {
          const el = document.createElement("ul");
          el.className = "el-cascader-menu__list";
          el.style =
            "border-top: solid 1px #E4E7ED; padding:0; color: #606266;";
          el.innerHTML = `<li class="el-cascader-node" >
                            <span class="el-cascader-node__label" style='color:#1267FA'>新建场景</span>
                            </li>
                        `;
          const el2 = document.createElement("ul");
          el2.className = "el-cascader-menu__list";
          el2.innerHTML = `<li class="el-cascader-node" >
                            <span class="el-cascader-node__label" style='color:#1267FA'>管理</span>
                            </li>
                        `;
          popper.appendChild(el);
          popper.appendChild(el2);
          el2.onclick = () => {
            onClick2 && onClick2();
            if (ref.toggleDropDownVisible) {
              ref.toggleDropDownVisible(false);
            } else {
              ref.visible = false;
            }
          };
          el.onclick = () => {
            // 底部按钮的点击事件 点击后想触发的逻辑也可以直接写在这
            onClick && onClick();
            // 以下代码实现点击后弹层隐藏 不需要可以删掉
            if (ref.toggleDropDownVisible) {
              ref.toggleDropDownVisible(false);
            } else {
              ref.visible = false;
            }
          };
        }
      }
    },
    // 表格前多选
    handleSelectionChange(val) {
      //   this.selShow = false;
      this.multipleSelection = val;
      if (val.length !== 0) {
        // this.chargeId = val[0].id;
        this.drawerData = val[0];
      }
      if (val.length == 0) {
        this.selShow = true;
      }
      if (val.length > 1) {
        this.disabled = true;
      } else {
        this.disabled = false;
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getList();
    },
    // 打开高级筛选
    screen() {
      this.dialog.dialogScreenVisible = true;
    },
    //   新建任务
    newTask() {
      this.dialog.newLoopTaskShowEject = true;
      this.$nextTick(function () {
        this.dialog.newLoopTaskShow = true;
      });
    },
    //   搜索
    search() {
      this.getList();
    },
    // 获取表格数据
    getList(val) {
      ruleList({
        sceneId: val,
        identification: this.input,
        pageSize: this.pageSize,
        pageNum: this.pageNum,
      }).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    // 判断是否drawer
    showH(item, index) {
      let bole = false;
      if (item.label === "任务名称") {
        bole = true;
      }
      if (!item.value) bole = false;
      return bole;
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  height: 74px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ececec;
  .bread {
    display: flex;
    align-items: center;
    margin-left: 5px;
    padding: 0 10px;
    height: 30px;
    display: flex;
    vertical-align: middle;
    height: 74px;
    border-bottom: 2px solid #1267fa;
    img {
      height: 100%;
    }
    i {
      font-size: 30px;
    }
    p {
      text-align: center;
      //   margin-left: 10px;
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      //   color: #2b2b2b;
    }
  }
  .ipt {
    display: flex;
  }
  .btn {
    .el-button {
      margin-right: 20px;
    }
  }
}
.chooise {
  margin-top: 20px;
  .sel {
    margin: 0 14px;
  }
  .sel-sence {
    margin-right: 20px;
  }
  .el-button {
    font-size: 14px;
    /deep/ span {
      margin-left: 5px;
    }
  }
}
.el-pagination {
  margin-top: 24px;
  /deep/ .el-pagination__total {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    margin-right: 25px;
  }
  /deep/ .btn-prev {
    width: 80px;
    height: 40px;
    text-align: center;
  }
  /deep/ .btn-next {
    width: 80px;
    height: 40px;
    text-align: center;
  }
  /deep/ .number {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .el-input__inner {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .more {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .el-pagination__jump {
    .el-input__inner {
      width: 40px;
      height: 40px;
    }
  }
}
.scenF {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  .scenF-content {
    display: flex;
    margin-left: 10px;
    margin-top: 10px;
    .scenF-detail {
      height: 36px;
      line-height: 36px;
      padding-left: 20px;
      padding-right: 20px;
      border: 1px solid #9aa1af;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #666666;
      .el-icon-circle-close {
        transform: translateY(3px);
      }
    }
  }
}
.sel {
  margin-top: 20px;
  span {
    margin-right: 40px;
  }
}
.cell {
  span {
    margin-right: 10px;
    color: #1267fa;
    cursor: pointer;
  }
  span:last-child {
    color: red;
  }
}
</style>